package com.mmall.controller.portal;

import com.github.pagehelper.PageInfo;
import com.mmall.common.IConst;
import com.mmall.common.ResponseInfo;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Shipping;
import com.mmall.pojo.User;
import com.mmall.service.IShippingService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 收货地址模块控制层
 * Created by qezhhnjy on 2017/10/3-11:41.
 */
@Controller
@RequestMapping("/shipping/")
public class ShippingController {

    @Resource
    private IShippingService iShippingService;

    /**
     * 新增收货地址
     *
     * @param session  当前会话
     * @param shipping 前端返回的收货地址对象信息
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "add.do", method = RequestMethod.POST)
    public ServerResponse add(HttpSession session, Shipping shipping) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), ResponseInfo.NEED_LOGIN.getDescription());
        return iShippingService.add(user.getId(), shipping);
    }

    /**
     * 删除当前用户的指定收货地址,要使用xml中的同时判定主键id和用户id的sql方法.横向越权.
     *
     * @param session    当前回话
     * @param shippingId 收货地址ID
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "delete.do", method = RequestMethod.POST)
    public ServerResponse delete(HttpSession session, Integer shippingId) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), ResponseInfo.NEED_LOGIN.getDescription());
        return iShippingService.delete(user.getId(), shippingId);
    }

    /**
     * 更新收货地址信息
     *
     * @param session  当前会话
     * @param shipping 更新后封装的收货地址信息
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "update.do", method = RequestMethod.POST)
    public ServerResponse update(HttpSession session, Shipping shipping) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), ResponseInfo.NEED_LOGIN.getDescription());
        return iShippingService.update(user.getId(), shipping);
    }

    /**
     * 查询指定id的收货地址
     *
     * @param session    当前会话
     * @param shippingId 需要查询的收货地址id
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "select.do", method = RequestMethod.POST)
    public ServerResponse<Shipping> select(HttpSession session, Integer shippingId) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), ResponseInfo.NEED_LOGIN.getDescription());
        return iShippingService.select(user.getId(), shippingId);
    }

    @RequestMapping(value = "select2.do", method = RequestMethod.POST)
    public String select2(HttpSession session, Integer shippingId, Model model) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return "shipping_modify";
        model.addAttribute("shipping", iShippingService.select(user.getId(), shippingId).getData());
        return "shipping_modify";
    }

    /**
     * 查询当前用户的所有收货地址信息并分页返回
     *
     * @param pageNum  当前页数
     * @param pageSize 每页数量
     * @param session  当前会话
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "list.do", method = RequestMethod.POST)
    public ServerResponse<PageInfo<Shipping>> list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpSession session) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), ResponseInfo.NEED_LOGIN.getDescription());
        return iShippingService.list(user.getId(), pageNum, pageSize);
    }

    @RequestMapping(value = "list2.do", method = RequestMethod.POST)
    public String list2(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                        HttpSession session, Model model) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return "shipping";
        model.addAttribute("district", iShippingService.list(user.getId(), pageNum, pageSize).getData());
        return "shipping";
    }
}
